library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use WORK.constants.all;

entity COUNTER32 is
	port(
		clk				: in std_logic;
		reset			: in std_logic;
		
		load			: in std_logic;
		go_ok			: in std_logic;
		seed			: in std_logic_vector(DATA_SIZE-1 downto 0);
		mod32			: out std_logic;
		end_of_count	: out std_logic
	);
end COUNTER32;


architecture BEHAVIORAL of COUNTER32 is
signal temp_int : integer;
--signal temp_mod32 : std_logic;
signal limit_int	: integer;
begin
	-- purpose : counts from 0 up to a limit initialized with <seed> signals
	-- type    : Behavioral
	P_counter : process(clk, reset, load)
	begin
		if reset = '1' then
			temp_int			<= 1;
			limit_int		<= conv_integer(unsigned(seed));			
		elsif ( clk'event and clk = '1' ) then
			if load='1' then
				temp_int		<= 1;
				limit_int	<= conv_integer(unsigned(seed));
			else
				if (go_ok = '1') and ( temp_int <= limit_int ) then
					temp_int	<= temp_int + 1;
				end if;
			end if;
		end if;
	end process;

	-- purpose : alert the external world after 32 rounds by setting <mod32> to 0 for 1 clock cycle.
	-- type    : Behavioral
	P_mod32 : process(temp_int)
	begin
		if temp_int mod 32 = 0 then
			mod32 <= '0'; --*
		else
			mod32 <= '1'; --*
		end if;
	end process;
	
	-- purpose : alert the external world when the counter reached the limit value by setting <end_of_count> to 0.
	P_end_of_count : process(temp_int, limit_int)
	begin
		if temp_int < limit_int then
			end_of_count <= '1';
		else
			end_of_count <= '0';
		end if;
	end process;
end;
